package com.project.auth.controller.process;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.auth.model.process.Process;
import com.project.auth.model.process.ProcessTemplate;
import com.project.auth.model.process.ProcessType;
import com.project.auth.service.ProcessService;
import com.project.auth.service.ProcessTemplateService;
import com.project.auth.service.ProcessTypeService;
import com.project.auth.service.SysUserService;
import com.project.auth.vo.process.ApprovalVo;
import com.project.auth.vo.process.ProcessFormVo;
import com.project.auth.vo.process.ProcessVo;
import com.project.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author XCM
 * @Date 2023/06/02 16:22
 */
@Api(tags = "审批流管理")
@RestController
@RequestMapping("/admin/process")
@CrossOrigin//跨域
public class UserProcessController {
    @Autowired
    private ProcessTypeService processTypeService;
    @Autowired
    private ProcessTemplateService processTemplateService;
    @Autowired
    private ProcessService processService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询所有审批分类和分类下的审批模板
     *
     * @return
     */
    @GetMapping("findProcessType")
    public Result findProcessType() {
        List<ProcessType> list = processTypeService.findProcessType();
        return Result.ok(list);
    }

    @ApiOperation(value = "获取审批模板")
    @GetMapping("getProcessTemplate/{processTemplateId}")
    public Result get(@PathVariable Long processTemplateId) {
        ProcessTemplate template = processTemplateService.getById(processTemplateId);
        return Result.ok(template);
    }

    @ApiOperation(value = "启动流程")
    @PostMapping("startUp")
    public Result startUp(@RequestBody ProcessFormVo formVo) {
        processService.startUp(formVo);

        return Result.ok();
    }

    @ApiOperation(value = "待处理")
    @GetMapping("findPending/{page}/{limit}")
    public Result findPending(@PathVariable Long page, @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageList = processService.findPending(pageParam);

        return Result.ok(pageList);
    }

    /**
     * 查看审批详情
     *
     * @param id
     * @return
     */
    @GetMapping("show/{id}")
    public Result show(@PathVariable Long id) {
        Map<String, Object> map = processService.show(id);
        return Result.ok(map);
    }

    /**
     * 审批
     */
    @ApiOperation(value = "审批")
    @PostMapping("approve")
    public Result approve(@RequestBody ApprovalVo approvalVo) {
        processService.approve(approvalVo);
        return Result.ok();
    }

    @ApiOperation(value = "已处理")
    @GetMapping("findProcessed/{page}/{limit}")
    public Result findProcessed(@PathVariable Long page, @PathVariable Long limit) {
        Page<Process> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageList = processService.findProcessed(pageParam);

        return Result.ok(pageList);
    }

    @ApiOperation(value = "已发起")
    @GetMapping("/findStarted/{page}/{limit}")
    public Result findStarted(@PathVariable Long page, @PathVariable Long limit) {
        Page<ProcessVo> pageParam = new Page<>(page, limit);
        IPage<ProcessVo> pageList = processService.findStarted(pageParam);
        return Result.ok(pageList);
    }
}
